#include "RecorderWindow.h"
#include <QKinectLib/KinectSensor.h>

RecorderWindow::RecorderWindow (QWidget *parent) : QMainWindow (parent)
{
	ui.setupUi(this);
}

RecorderWindow::~RecorderWindow ()
{
	for (int i = 0; i < _sensorPtrs.size (); ++i) {
		delete _sensorPtrs[i];
		delete _recorderPtrs[i];
	}
}

void RecorderWindow::handleNewFrame ()
{
	KinectSensor* pSensor = (KinectSensor*)sender ();
	for (int i = 0; i < _sensorPtrs.size (); ++i) {
		if (pSensor == _sensorPtrs[i])
			_recorderPtrs[i]->writeFrame ();
	}
}

void RecorderWindow::showEvent (QShowEvent *)
{
	int sensorCount = KinectSensor::getSensorCount ();
	if (sensorCount == 0)
		exit (0);
	for (int i = 0; i < sensorCount; ++i) {
		KinectSensor* pSensor = new KinectSensor (QString ("Kinect %1").arg (i),
			NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_640x480,
			NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480,
			QString ("../Data/Intrinsic/%1.int").arg (i));
		connect (pSensor, SIGNAL(newFrameReady(void)),
			this, SLOT(handleNewFrame(void)));
		pSensor->start ();

		SensorDataFileOutput* pRecorder = new SensorDataFileOutput (
			QString ("../Data/%1.krec").arg (i), *pSensor);
		_sensorPtrs.push_back (pSensor);
		_recorderPtrs.push_back (pRecorder);
	}
}
